//
//  VerificationCodeViewController.m
//  FreeTravel
//
//  Created by 徐康 on 16/8/27.
//  Copyright © 2016年 KX. All rights reserved.
#import "PersonalInformationViewController.h"
#import "FreeTravelMacro.h"
#import "VerificationCodeViewController.h"
#import "Masonry.h"
#import "FreeTravelNetwork.h"

@interface VerificationCodeViewController ()
@property (nonatomic, strong) UIButton *resendButton;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation VerificationCodeViewController
{
    int countDown;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    self.navigationItem.title = @"手机号注册";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    self.view.backgroundColor = [UIColor colorWithRed:0.11 green:0.68 blue:0.45 alpha:1.00];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"sdfasadfas"] style:UIBarButtonItemStylePlain target:self action:@selector(goLogin)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    UILabel *inputLabel = [[UILabel alloc]init];
    [self.view addSubview:inputLabel];
    inputLabel.textAlignment = NSTextAlignmentCenter;
    inputLabel.text = [NSString stringWithFormat:@"验证短信已发送至%@",self.phoneNumber];
    [inputLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.equalTo(self.view).multipliedBy(0.7);
        make.height.equalTo(self.view).multipliedBy(0.05);
        make.centerY.equalTo(self.view).multipliedBy(0.4);
    }];
    
    UIButton *goNextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:goNextButton];
    [goNextButton setTitle:@"下一步" forState:UIControlStateNormal];
    [goNextButton setTitleColor:COLORFORFREETRAVEL forState:UIControlStateNormal];
    goNextButton.backgroundColor = [UIColor whiteColor];
    goNextButton.layer.cornerRadius = 5;
    goNextButton.layer.masksToBounds = YES;
    [goNextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(44);
        make.center.mas_equalTo(self.view);
    }];
    [goNextButton addTarget:self action:@selector(goNext) forControlEvents:UIControlEventTouchUpInside];
    
    self.verficationTextField = [[UITextField alloc]init];
    [self.view addSubview:self.verficationTextField];
    self.verficationTextField.attributedPlaceholder = [[NSAttributedString alloc]initWithString:@"输入验证码" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    self.verficationTextField.textAlignment = NSTextAlignmentCenter;
    [self.verficationTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view).multipliedBy(0.5);
        make.centerX.equalTo(self.view).multipliedBy(0.6);
        make.width.equalTo(self.view).multipliedBy(0.4);
        make.height.equalTo(self.view).multipliedBy(0.07);
    }];
   
    //添加计时器,倒计时重新发送
    
    countDown = 60;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
    
    self.resendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:_resendButton];
    
    [self.resendButton setTitle:@"重新发送(60)" forState:UIControlStateNormal];
    [self.resendButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.resendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.centerY.equalTo(self.verficationTextField);
        make.width.equalTo(self.view).multipliedBy(0.3);
        make.centerX.equalTo(self.view).multipliedBy(1.3);
    }];
    self.resendButton.backgroundColor = [UIColor colorWithRed:0.35 green:0.76 blue:0.60 alpha:1.00];
    self.resendButton.layer.cornerRadius = 3;
    self.resendButton.layer.masksToBounds = YES;
    [self.resendButton addTarget:self action:@selector(resendVerification:) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *whiteLine = [[UIView alloc]init];
    [self.view addSubview:whiteLine];
    whiteLine.backgroundColor = [UIColor whiteColor];
    [whiteLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(2);
        make.width.centerX.equalTo(self.verficationTextField);
        make.top.equalTo(self.verficationTextField.mas_bottom);
    }];
    
}
- (void)countDown{
    countDown--;
    NSString *count = [NSString stringWithFormat:@"重新发送%d",countDown];
    [self.resendButton setTitle:count forState:UIControlStateNormal];
    if(countDown == 0){
        [self.resendButton setTitle:@"重新发送" forState:UIControlStateNormal];
        [self.timer invalidate];
    }
}
- (void)resendVerification:(UIButton *)button{
    
    [FreeTravelNetwork GET:[NSString stringWithFormat:@"http://open.qyer.com/qyer/user/active_code?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&country_code=86&mobile=%@&page=1&track_app_channel=App%%2520Store&track_app_version=7.0.1&track_device_info=iPhone%%25205s&track_deviceid=E9CA001A-5603-4CA8-B2EA-2BE34F691AC6&track_os=ios%%25209.3.4&v=1",self.phoneNumber] andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        countDown = 60;
        [self.timer fire];
        
        
        
        
    } andFailureBlock:^(NSError *error) {
        
    }];

}
- (void)goLogin{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)goNext{

    PersonalInformationViewController *vc = [[PersonalInformationViewController alloc]init];
    vc.verification = self.verficationTextField.text;
    vc.phoneNum = self.phoneNumber;
    
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
